package com.zingrow.web.report.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zingrow.common.utils.CommonUtils;
import com.zingrow.web.configure.iformation.mapper.IformationMapper;
import com.zingrow.web.configure.iformation.model.Iformation;
import com.zingrow.web.customer.mapper.GroupSchemeMapper;
import com.zingrow.web.report.mapper.RptModuleInfoMapper;
import com.zingrow.web.report.model.RptModule;
import com.zingrow.web.report.model.RptModuleInfo;
import com.zingrow.web.report.response.PlanItem;
import com.zingrow.web.report.service.IRptModuleService;

@Service
public class RptModuleServiceImpl implements IRptModuleService {
    @Autowired
    private RptModuleInfoMapper rptModuleInfoMapper;
    @Autowired
    private IformationMapper iformationMapper;
    @Autowired
	private GroupSchemeMapper schemeMapper;

    @Override
    public RptModuleInfo selectModuleInfo(Integer modelId, Integer moduleId) {
        Map<String, Object> map = new HashMap<>();
        map.put("modelId", modelId);
        map.put("moduleId", moduleId);
        return rptModuleInfoMapper.selectModuleInfo(map);
    }
    //查询模板模块相对应的方案
    @Override
    public List<RptModuleInfo> selectBymid(Integer modelId) {
    	List<RptModuleInfo> mlist = new ArrayList<RptModuleInfo>();
    	Map<String,Object> map = new HashMap<String, Object>();
    	//根据模板ID查询模块
    	List<RptModuleInfo> list = rptModuleInfoMapper.selectBymid(modelId);
    	RptModuleInfo module = new RptModuleInfo();
    	for (RptModuleInfo rptModuleInfo : list) {
    		//根据模块ID查询模块名称
    		RptModule module2 = rptModuleInfoMapper.selectmoduleid(rptModuleInfo.getModuleId());
    		if (null!=module2) {
    			map.put("modelId", modelId);
            	map.put("moduleId",rptModuleInfo.getModuleId());
            	//根据模板查询相应的方案
            	module = rptModuleInfoMapper.selectModuleInfo(map);
            	module.setModuleName(module2.getName());
			}
        	if (module.getPlanItems().size()>0) {
        		List<PlanItem> plist = module.getPlanItems();
            	for (PlanItem planItem : plist) {
            		//查询方案名称
            		Iformation iform = iformationMapper.queryByID(planItem.getPlanId());
            		//判断对象是否为空
            		if(CommonUtils.isObjectNotNull(iform)) {
            			planItem.setPlanName(iform.getIfm_name());
            		}
    			}
        	}
        	mlist.add(module);
		}
		return mlist;
    }
   
  /*  //修改
    @Override
    public boolean update(Integer groupId,Integer modelId,String moduleId,List<String> planId){
    	//分割模块ID
    	List<String> midlist = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(moduleId);
    	int count = 0;
    	for (String mids : midlist) {
			for (String fid : planId) {
				List<String> pid = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(fid);
				for (String pids : pid) {
					//根据方案ID查询该方案是否是敏感
					GroupScheme scheme = schemeMapper.selectgidfid(groupId,Integer.valueOf(pids));
					RptModuleInfo module = new RptModuleInfo();
					module.setPlanId(Integer.valueOf(pids));
					module.setIsSensitive(scheme.getIfmIsSensitivity());
					module.setModelId(modelId);
					module.setModuleId(Integer.valueOf(mids));
					//修改
					count = rptModuleInfoMapper.updateByPrimaryKey(module);
					//return count>0;
				}
			}
		}
    	return count > 0;
    }*/
    
    //根据方案ID删除数据
    @Override
  	public boolean deleteplanId(Integer planId) {
  		return rptModuleInfoMapper.deleteplanId(planId)>0;
  	}
    //根据方案ID查询该方案被哪些模板使用
	@Override
	public List<RptModuleInfo> selectByplanId(Integer planId) {
		return rptModuleInfoMapper.selectByplanId(planId);
	}
	
	@Override
	public List<RptModuleInfo> queryGroupIdIfmId(Integer groupId,Integer moduleId) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("groupId", groupId);
		map.put("moduleId", moduleId);
		return rptModuleInfoMapper.queryGroupIdIfmId(map);
	}
	@Override
	public boolean updateByPrimaryKey(RptModuleInfo module) {
		return rptModuleInfoMapper.updateByPrimaryKey(module)>0;
	}
    /**
     * 根据组织ID查询数据
     */
	@Override
	public List<RptModuleInfo> querymodelId(Integer model_id)
	{
		return rptModuleInfoMapper.querymodelId(model_id);
	};
	@Override
	public List<RptModule> query()
	{
		return rptModuleInfoMapper.query();
	};
	
/*	@Override
	public boolean delBygroupIdplanId(Integer moduleId,Integer groupId, Integer planId) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("moduleId", moduleId);
		map.put("groupId", groupId);
		map.put("planId", planId);
		return rptModuleInfoMapper.delBygroupIdplanId(map)>0;
	}
	
	@Override
	public boolean insertmodule(RptModuleInfo module) {
		return rptModuleInfoMapper.insertmodule(module)>0;
	}
	@Override
	public int queryGroupIdmoduleId(Integer groupId, Integer moduleId) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("groupId", groupId);
		map.put("moduleId", moduleId);
		return rptModuleInfoMapper.queryGroupIdmoduleId(map);
	}
	@Override
	public int selectBygIdpIdmuId(Integer groupId,Integer moduleId,Integer planId) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("groupId", groupId);
		map.put("moduleId", moduleId);
		map.put("planId", planId);
		return rptModuleInfoMapper.selectBygIdpIdmuId(map);
	}*/
    
	
}
